//
//  XYRetweetedView.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 16/1/1.
//  Copyright © 2016年 cn.weibo. All rights reserved.
//

import UIKit
import SnapKit
class XYRetweetedView: UIView {

    // 保存底部约束
    var retweetedViewBottomConstraint: Constraint?
    var statusViewModel: XYStatusViewModel? {
        
        didSet {    
            
            textLabel.attributeString = statusViewModel?.retweetMatchAttributeText
            textLabel.matchResults = statusViewModel?.retweetMatchResults
            // 卸载之前约束
            retweetedViewBottomConstraint?.uninstall()
            
            if let pictures = statusViewModel?.status?.retweeted_status?.pic_urls where pictures.count > 0 {
                
                homePictureView.hidden = false
                homePictureView.pictures = pictures
                homePictureView.viewModel = statusViewModel
                // TODO: 更新约束为图片的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetedViewBottomConstraint = make.bottom.equalTo(homePictureView).offset(XYStatusCellMargin).constraint
                })
            }else{
                homePictureView.hidden = true
                // 更新约束为内容的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    retweetedViewBottomConstraint = make.bottom.equalTo(textLabel).offset(XYStatusCellMargin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpSubViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpSubViews() {
        addSubview(textLabel)
        addSubview(homePictureView)
        backgroundColor = UIColor.init(white: 0.97, alpha: 1)
        // 添加约束
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(XYStatusCellMargin)
            make.top.equalTo(self).offset(XYStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -XYStatusCellMargin)
        }
        homePictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLabel.snp_bottom)
            make.leading.equalTo(textLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
            retweetedViewBottomConstraint = make.bottom.equalTo(homePictureView).offset(XYStatusCellMargin).constraint
        }
    }
    // 懒加载控件
    // 内容
    private lazy var textLabel: XYStatusContentLabel = {
        
        let textLabel = XYStatusContentLabel()
        textLabel.font = UIFont.systemFontOfSize(XYStatusCellFontSize)
        return textLabel
    }()
    private lazy var homePictureView:XYHomePictureView = {
        
        let homePictureView = XYHomePictureView()
        return homePictureView
    }()
}
